package com.责任链模式;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 自定义“责任人”
 * @author Fang Ruichuan
 * @date 2022-09-03 13:30
 */
public class CustomInterceptor implements Ratify {

    @Override
    public Result deal(Chain chain) {
        Request request = chain.request();
        System.out.println("CustomInterceptor=====>" + request.toString());
        AtomicReference<Result> result = new AtomicReference<>(new Result(true, "同意请假"));
        Optional.ofNullable(request.getReason()).ifPresent(reason -> {
            if ("事假".equals(reason)) {
                Request newRequest = new Request.Builder()
                        .newRequest(request)
                        .customInfo(request.getName() + "请的是事假，而且很着急，请领导重视一下")
                        .build();
                System.out.println("CustomInterceptor=====>转发请求");
                result.set(chain.proceed(newRequest));
            }
        });
        return result.get();
    }

}
